var app = getApp()
Page({
  data: {
    ableAddFriend: false,
    show: false,
     deadline1:'',
      deadline2:'',
    have_pay: {
      _openid: '',
      title: '',
      isSell: false, //true 表示卖
      tag: '0', //分类
      description: "",
      price: '',
      deadline:'',
      phone: 0,
      QQGroup:0,
      copy:'',
      publish_time: "",
      time: '',
      formId: ""
    }
  },
  onLoad() {

  },
  onReady() {
    this.setData({
      show: true
    })
  },
  onUnload() {
    //console.log("sd")
  },  
  DateChange(e) {
    this.setData({
      'deadline1': e.detail.value
    })
  },
  TimeChange(e) {
    this.setData({
      'deadline2': e.detail.value
    })
  },

  doSubmit(e) {
     var time = new Date()
      var theTime = time.getFullYear() + '年' + (time.getMonth() + 1) + "月" + time.getDate() + "日 "
      if(time.getHours()<10){
        theTime+='0'
      }
      theTime+=time.getHours()+":"
      if(time.getMinutes()<10){
        theTime+='0'
      }
      theTime+=time.getMinutes()
    this.setData({
      'have_pay.deadline':this.data.deadline1+" "+ this.data.deadline2,
      'have_pay._openid': app.globalData.openid,
      'have_pay.formId': e.detail.formId,
      'have_pay.publish_time': new Date().getTime(),
      'have_pay.time':theTime
    })
    if (this.data.have_pay.title < 3) {
      qq.showToast({
        icon: "none",
        title: "标题不能少于三个字哦~"
      })
    }
    else if (this.data.have_pay.description.length < 3) {
      qq.showToast({
        icon: "none",
        title: "描述信息不能小于三个字哦~"
      })
    }else if(this.data.have_pay.price.length<1){
      qq.showToast({
        icon:'none',
        title:'请输入付费信息'
      })
    }else if(this.data.deadline1==''||this.data.deadline2==""){
        qq.showToast({
            icon:'none',
            title:'请选择截止日期和时间'
        })
    } else {
      this.addToDB()
    }
  },
  
  addToDB() {
    var data = JSON.stringify(this.data.have_pay)
    qq.request({
      url: 'https://api.weixin.qq.com/tcb/databaseadd?access_token=' + app.globalData.access_token, // 仅为示例，并非真实的接口地址
      header: {
        'content-type': 'application/json' // 默认值
      }, method: "post",
      data: {
        "env": app.globalData.myEnv,
        "query": "db.collection(\"have_pay\").add({\
            data: "+ data + "\
        })"
      },
      success(res) {
        qq.hideLoading()
        qq.showToast({
          title: "发布成功"
        })
        qq.navigateBack()
      }
    })
  },
  inputTitle(e) {
    this.setData({
      'have_pay.title': e.detail.value
    })
  },
  input1(e) {
    this.setData({
      'have_pay.description': e.detail.value
    })
    // //console.log(e)
  },
  inputPhone(e) {
    // //console.log(e)
    this.setData({
      'have_pay.phone': e.detail.value
    })
  },
  inputPrice(e) {
    //console.log(e)
    this.setData({
      'have_pay.price': e.detail.value
    })
  },
    inputQQGroup(e) {
    // //console.log(e)
    this.setData({
      'have_pay.QQGroup': e.detail.value
    })
  },
    inputCopy(e) {
    this.setData({
      'have_pay.copy': e.detail.value
    })
  },
  tagChange(e) {
    this.setData({
      'have_pay.tag': e.currentTarget.id
    })
    // //console.log(e)
  },
  radioChange(e) {
    //console.log(e)
    var isSell = e.detail.value == '1' ? true : false
    this.setData({
      'have_pay.isSell': isSell
    })
  }
})